<!--
 * @Author: wangming
 * @Date: 2020-09-16 17:49:22
 * @LastEditors: wangming
 * @LastEditTime: 2020-12-09 16:40:27
 * @Description: file content
-->
<template>
  <el-input
    :style="{ width: config.__config.width + '%', height: '32px' }"
    class="g9"
    readonly
    :value="value.label"
    placeholder="暂无数据"
  ></el-input>
</template>

<script>
import myMixin from "@/pageComponents/myMixin";
var moment = require("moment");
import request from "@/utils/request.js";
export default {
  mixins: [myMixin],
  data() {
    return {};
  },
  watch: {
    timelength(v) {
      if (!v) {
        this.value.label = "0天";
        this.value.seconds = 0;
        this.value.value = 0;
        return;
      }
      if (!!this.config.__config.rangeRule) {
        this.getTimeMsgToApi();
      } else {
        let [start, end] = v.split(",");
        //按年选择
        if (end?.length === 4) {
          end = moment(end, "YYYY").add(1, "year");
        }
        //按月选择
        if (end?.length === 7) {
          end = moment(end, "YYYY-MM").add(1, "month");
        }
        //按天选择
        if (end?.length === 10) {
          end = moment(end).add(1, "day");
        }
        let stemp = moment(end || undefined).diff(start || undefined);
        this.value.label = this.getTimeMsg(stemp);
        this.value.seconds = stemp;
        this.value.value = (stemp / (3600 * 24 * 1000)).toFixed(2);
      }
    },
  },
  methods: {
    getTimeMsgToApi() {
      setTimeout(() => {
        // this.rangeRuleTime = this.getTimeMsg(435660000);
      }, 500);
    },
    getTimeMsg(stemp) {
      let days = parseInt(stemp / (3600 * 24 * 1000));
      let hours = parseInt((stemp % (3600 * 24 * 1000)) / (3600 * 1000));
      let minutes = (stemp % (3600 * 1000)) / (60 * 1000);
      let str =
        days +
        "天" +
        (hours > 0 ? hours + "小时" : "") +
        (minutes > 0 ? minutes + "分钟" : "");
      return str;
    },
  },
  computed: {
    timelength() {
      if (!!this.form?.[this.config.__config.originModelName]) {
        return this.form?.[this.config.__config.originModelName].replace(
          "~",
          ","
        );
      } else {
        return "";
      }
    },
  },
};
</script>

<style lang="scss" scoped></style>
